package com.service.impl;

import com.dao.BrandDao;
import com.dao.ProductTypeDao;
import com.dao.impl.BrandDaoImpl;
import com.dao.impl.ProductTypeDaoImpl;
import com.pojo.Brand;
import com.pojo.PageBean;
import com.service.BrandService;

import java.util.List;

public class BrandSercviceImpl implements BrandService {
    private BrandDao dao = new BrandDaoImpl();

    /**
     * 查询分页方法
     * @param limit
     * @param current
     * @return
     */
    @Override
    public PageBean<Brand> findAll(int limit, int current) {
        int cOunt = dao.findCOunt();//获取到总函行数
        List<Brand> list = dao.findAll((current - 1) * limit, limit);
        ProductTypeDao typeDao = new ProductTypeDaoImpl();
        for (Brand brand : list) {
            brand.setProductType(typeDao.findByID(brand.getBrandType()));
        }
        PageBean<Brand> page = new PageBean<>();
        page.setTotal(cOunt);
        page.setData(list);
        page.setCurrent(current);

        return page;
    }

    /**
     * 新增方法
     * @param brand
     * @return
     */
    @Override
    public boolean addBrand(Brand brand) {
        return dao.addBrand(brand)>0;
    }

    /**
     * 修改方法
     * @param brand
     * @return
     */
    @Override
    public boolean updateBrand(Brand brand) {
        return dao.updateBrand(brand)>0;
    }

    /**
     * 根据ID删除一条数据
     * @param id
     * @return
     */
    @Override
    public boolean deleteBrand(String id) {

        return dao.deleteBrand(id)>0;
    }

    /**
     * 根据id查询一挑数据
     * @param id
     * @return
     */
    @Override
    public Brand findByID(String id) {
        Brand brand = dao.findByID(id);
        brand.setProductType(new ProductTypeDaoImpl().findByID(brand.getBrandType()));
        return brand;
    }

    /**
     * 根据名字判断是否有重复数据
     * @param name
     * @return
     */
    @Override
    public Brand findByName(String name) {
        return dao.findByName(name);
    }

    @Override
    public List<Brand> findByBrandType(String productTypeName) {

        return dao.findByBrandType(new ProductTypeDaoImpl().findByName(productTypeName).getId());
    }


}
